﻿#include "TradeDetailJson.h"
#include "Tools.h"
#include "JsonTools.h"

void TDJson::StrategyDataToJson(Json::Value &outValue, const TD::StrategyBaseData &pStrategyData)
{
	outValue["OrderStrategy"] = pStrategyData.OrderStrategy;
	outValue["StrategyValue"] = pStrategyData.StrategyValue;
	outValue["StrategyUTM"] = Tools::DateTimeStamp2Str(pStrategyData.StrategyUTM);
	outValue["RealValue"] = pStrategyData.RealValue;
	outValue["RealUTM"] = Tools::DateTimeStamp2Str(pStrategyData.RealUTM);
}

void TDJson::Json2StrategyData(TD::StrategyBaseData &outStrategyData, const Json::Value& pJValue)
{
	outStrategyData.OrderStrategy = JsonTool::ToInt(pJValue, "OrderStrategy", 0);
	outStrategyData.StrategyValue = JsonTool::ToFloat(pJValue, "StrategyValue");
	std::string strategyTime = JsonTool::ToString(pJValue, "StrategyUTM");
	outStrategyData.StrategyUTM = Tools::DateTimeStr2Stamp(strategyTime);
	outStrategyData.RealValue = JsonTool::ToFloat(pJValue, "RealValue");
	std::string realTime = JsonTool::ToString(pJValue, "RealUTM");
	outStrategyData.RealUTM = Tools::DateTimeStr2Stamp(realTime);
}

void TDJson::TradeOrderToJson(Json::Value &outValue, const TD::Order &pOrder)
{
	Json::Value &pEnterVal = outValue["Enter"] = Json::Value();
	StrategyDataToJson(pEnterVal, pOrder.EnterData);

	Json::Value &pExitVal = outValue["Exit"] = Json::Value();
	StrategyDataToJson(pExitVal, pOrder.ExitData);
}

void TDJson::JsonToTradeOrder(TD::Order &outOrder, const Json::Value& pJValue)
{
	if (pJValue.isMember("Enter"))
	{
		Json2StrategyData(outOrder.EnterData, pJValue["Enter"]);
	}
	if (pJValue.isMember("Exit"))
	{
		Json2StrategyData(outOrder.ExitData, pJValue["Exit"]);
	}
}